﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using Microsoft.Practices.ServiceLocation;
using Sok.Munkae.ApplicationServices;
using Sok.Munkae.ApplicationServices.DTO;
using Sok.Munkae.Web.PresentationServices;

namespace Sok.Munkae.Web
{
    public class ProjectListViewModel
    {
        public OrganizationDTO Organization { get; set; }
        public UserDTO CurrentUser { get; set; }
        public IList<ProjectDTO> ProjectList { get; set; }
        public string CurrentUserName { get { return CurrentUser != null ? CurrentUser.UserName : "[unknown]"; } }
        public string OrganizationName { get { return Organization != null ? Organization.Name : "[unknown]"; } }
        }

    public partial class _Default : Page
    {
        protected ProjectListViewModel model;
        private ProjectPresentationService presentationService;
        private IAuthenticationService authenticationService;

        protected void Page_Load(object sender, EventArgs e)
        {
            presentationService = ServiceLocator.Current.GetInstance<ProjectPresentationService>();
            authenticationService = ServiceLocator.Current.GetInstance<IAuthenticationService>();
            model = presentationService.ProjectListViewModel();
            BindProjectList(model);
        }

        private void BindProjectList(ProjectListViewModel viewModel)
        {
            projectListRepeater.DataSource = model.ProjectList;
            projectListRepeater.DataBind();
        }

        protected void SignoutButton_Click(object sender, EventArgs e)
        {
            authenticationService.SignOut();
        }
    }
}